/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_parse.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <pop3/pop3.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/pop3/config.h"

typedef struct __st_L7session_pop3_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    L7_transaction st_l7_trans;                       //数据
    int i_flag_data_type;                             //1: FRLOGIN_START 2:FRLOGIN_SUCC 3:FRLOGIN_FAIL 4 FRLOGOUT_START 5 FRLOGOUT_END 0 : other
    char *p_header_name;
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    char *p_user_name;
    struct timeval st_login_timestamp;
    char arr_session_id[APM_UUID_SIZE];
    int i_sign;                                       //标志位 0||1, 0:需要resp解析 1:不需要resp解析
    int i_flag_end;                                   //标志位 0||1, 0:req为PASS 1:req不为PASS
    int i_flag_response;                              //标志位 0||1, 0:response的code和string不必上报 1：需要上报
}L7session_pop3_private;

extern int __resp_parse(char *p_data, unsigned int ui_data_len, L7session_pop3_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn);
extern int pop3_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 0;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 13;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OO 1 56519\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 0;
    p_st_private->i_flag_response = 1;
    p_st_private->i_flag_data_type = 0;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_EQUAL(p_st_private->i_sign, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, 0);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case3(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 13;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OK 1 56519\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 0;
    p_st_private->i_flag_response = 1;
    p_st_private->i_flag_data_type = 0;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(p_st_private->i_sign, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, 0);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case4(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 29;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OK Name is a valid mailbox\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 0;
    p_st_private->i_flag_response = 0;
    p_st_private->i_flag_data_type = FRLOGIN_START;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    CU_ASSERT_EQUAL(p_st_private->i_sign, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, FRLOGIN_START);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case5(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 77;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OK Mailbox locked and ready SESSIONID=<mail.broada.com-24121-1427942444-1>\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 1;
    p_st_private->i_flag_response = 0;
    p_st_private->i_flag_data_type = FRLOGIN_START;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(p_st_private->i_sign, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, FRLOGIN_SUCC);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case6(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 38;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OK scan listing follows\r\n1 56519\r\n.\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 0;
    p_st_private->i_flag_response = 0;
    p_st_private->i_flag_data_type = 0;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(p_st_private->i_sign, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, 0);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case7(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    unsigned int ui_data_len = 5;
    L7session_pop3_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = frstrdup("+OK\r\n");
    if(p_data == NULL)
        goto ERROR;

    p_st_private = (L7session_pop3_private *)frmalloc(sizeof(L7session_pop3_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_pop3_private));
    p_st_private->i_sign = 0;
    p_st_private->i_flag_end = 0;
    p_st_private->i_flag_response = 0;
    p_st_private->i_flag_data_type = 0;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_data, ui_data_len, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(p_st_private->i_sign, 1);
    CU_ASSERT_EQUAL(p_st_private->i_flag_end, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_response, 0);
    CU_ASSERT_EQUAL(p_st_private->i_flag_data_type, FRLOGOUT_END);

    frfree(p_data);
    pop3_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_data)
        frfree(p_data);
    if(p_st_private)
        pop3_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

int add_suite__resp_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
